package com.abi${classpath}.service.impl;

import com.abi${classpath}.service.${targetName_Iii}Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * ${targetDesc}
 * </p>
 *
 * @author Andy.Tan
 */
@Service
public class ${targetName_Iii}ServiceImpl implements ${targetName_Iii}Service {

    @Autowired
    private ${tableName_Iii}Mapper ${tableName_iii}Mapper;


    /**
     * 新增${targetDesc}
     */
    @Override
    public void add${targetName_Iii}(Add${targetName_Iii}Req req){
        //校验入参
        //TODO

        //1-新增
        ${tableName_Iii} ${targetName_iii} = new ${tableName_Iii}();
        <#list columnList as temp>
        <#if temp.columnName_iii!="id" &&  temp.columnName_iii!="createTime" &&  temp.columnName_iii!="createBy" && temp.columnName_iii!="updateTime" && temp.columnName_iii!="updateBy" && temp.columnName_iii!="isDelete" >
        ${targetName_iii}.set${temp.columnName_Iii}(req.get${temp.columnName_Iii}());
        </#if>
        </#list>
        ${tableName_iii}Mapper.insert(${targetName_iii});

    }

    /**
     * 编辑${targetDesc}
     */
    @Override
    public void update${targetName_Iii}(Update${targetName_Iii}Req req){

        //1-查询${targetDesc}
        ${tableName_Iii} ${targetName_iii} = ${tableName_iii}Mapper.selectById(req.getId());
        AssertUtil.isNull(${targetName_iii},"${targetDesc}不存在");

        //2-修改${targetDesc}
        <#list columnList as temp>
        <#if temp.columnName_iii!="id" &&  temp.columnName_iii!="createTime" &&  temp.columnName_iii!="createBy" && temp.columnName_iii!="updateTime" && temp.columnName_iii!="updateBy" && temp.columnName_iii!="isDelete" >
        ${targetName_iii}.set${temp.columnName_Iii}(req.get${temp.columnName_Iii}());
        </#if>
        </#list>
        ${tableName_iii}Mapper.updateById(${targetName_iii});

    }

    /**
     * 分页查询${targetDesc}
     */
    @Override
    public List${targetName_Iii}Res list${targetName_Iii}(List${targetName_Iii}Req req){

        //1-拼接条件
        QueryWrapper<${tableName_Iii}> ${targetName_iii}Qw = new QueryWrapper<>();
        //${targetName_iii}Qw.like(StringUtils.isNotBlank(req.getXxx()), "xxx_xxx", "%" + req.getXxx() + "%");
        //${targetName_iii}Qw.eq(req.getXxx2() != null, "xxx_xxx2", req.getXxx2());

        //2-分页查询
        IPage<${tableName_Iii}> iPage = ${tableName_iii}Mapper.selectPage(PageUtil.createPage(req), ${targetName_iii}Qw);
        List<${tableName_Iii}> ${targetName_iii}List = iPage.getRecords();

        //封装出参
        List${targetName_Iii}Res res = new List${targetName_Iii}Res();
        //放入分页信息
        PageUtil.copyPageInfo(res, iPage);
        //放入出参列表
        List<List${targetName_Iii}Res.${targetName_Iii}Bean> ${targetName_iii}BeanList = PojoConverterUtils.copyList(${targetName_iii}List, List${targetName_Iii}Res.${targetName_Iii}Bean.class);
        res.set${targetName_Iii}BeanList(${targetName_iii}BeanList);

        return res;

    }

    /**
     * 查询${targetDesc}明细
     */
    @Override
    public Get${targetName_Iii}Res get${targetName_Iii}(Get${targetName_Iii}Req req){

        //1-查询${targetDesc}对象
        ${tableName_Iii} ${targetName_iii} = ${tableName_iii}Mapper.selectById(req.getId());
        AssertUtil.isNull(${targetName_iii}, "${targetDesc}不存在");

        //转化出参
        Get${targetName_Iii}Res res = PojoConverterUtils.copy(${targetName_iii}, Get${targetName_Iii}Res.class);

        return res;

    }

    /**
     * ${targetDesc}启用
     */
    @Override
    public void enable${targetName_Iii}(Enable${targetName_Iii}Req req){
        List<Long> ids = req.getIds();

        //循环启用
        for (Long id : ids) {
            //1-查询活动对象
            ${tableName_Iii} ${targetName_iii} = ${tableName_iii}Mapper.selectById(id);
            AssertUtil.isNull(${targetName_iii}, "${targetDesc}不存在");

            if (!ValidEnum.NOT_VALID.is(${targetName_iii}.getValid())) {
                continue;
            }

            //2-修改状态
            ${targetName_iii}.setValid(ValidEnum.VALID.getCode());
            ${tableName_iii}Mapper.updateById(${targetName_iii});
        }
    }

    /**
     * ${targetDesc}禁用
     */
    @Override
    public void disable${targetName_Iii}(Disable${targetName_Iii}Req req){
        List<Long> ids = req.getIds();

        //循环禁用
        for (Long id : ids) {
            //1-查询活动对象
            ${tableName_Iii} ${targetName_iii} = ${tableName_iii}Mapper.selectById(id);
            AssertUtil.isNull(${targetName_iii}, "${targetDesc}不存在");

            if (!ValidEnum.VALID.is(${targetName_iii}.getValid())) {
                return;
            }

            //2-修改状态
            ${targetName_iii}.setValid(ValidEnum.NOT_VALID.getCode());
            ${tableName_iii}Mapper.updateById(${targetName_iii});
        }
    }

}
